package top.ffhome.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import top.ffhome.common.exception.BusinessException;
import top.ffhome.common.page.PageRequest;
import top.ffhome.common.result.Result;
import top.ffhome.system.entity.SysRole;
import top.ffhome.system.service.SysRoleService;
import top.ffhome.util.bean.BeanUtil;

@CrossOrigin
@RestController
@RequestMapping("api/role")
@Api(tags = "系统角色控制器")
public class SysRoleController {
	@Autowired
	private SysRoleService roleService;

	@RequiresPermissions("system:user:view")
	@GetMapping("/getAll")
	@ApiOperation("获得所有角色列表")
	public Result getAll() {
		return Result.success(roleService.getAll());
	}

	@RequiresPermissions(value = { "system:role:add", "system:role:edit" }, logical = Logical.OR)
	@PostMapping("/save")
	@ApiOperation("保存角色")
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "sort", value = "排序", dataType = "Integer", paramType = "body"),
			@ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "menuIds", value = "菜单ID列表", paramType = "body", allowMultiple = true, dataType = "Long"),
			@ApiImplicitParam(name = "id", value = "角色ID,修改时必须", dataType = "Long", paramType = "body"),
			@ApiImplicitParam(name = "updateTime", value = "更新时间，乐观锁", dataType = "Date", paramType = "body") })
	public Result save(@RequestBody SysRole role) {
		List<String> fields = CollUtil.newArrayList("name", "sort", "status", "remark", "menuIds");
		if (!(role.getId() == null || role.getId() == 0)) {
			fields.add("id");
			fields.add("updateTime");
		}
		BeanUtil.constraintProperties(role, fields);
		if (role.getId() == null) {
			roleService.insert(role);
		} else {
			roleService.update(role);
		}
		return Result.success();
	}

	@RequiresPermissions("system:role:remove")
	@PostMapping("/delete")
	@ApiOperation("删除角色")
	@ApiImplicitParams(@ApiImplicitParam(name = "ids", value = "需删除的ID", required = true, paramType = "body", allowMultiple = true, dataType = "Long"))
	public Result delete(@RequestBody List<Long> ids) {
		try {
			roleService.delete(ids);
		} catch (BusinessException e) {
			return Result.fail(e.getMessage());
		}
		return Result.success();
	}

	@RequiresPermissions("system:role:view")
	@PostMapping("/get")
	@ApiOperation("获取单个角色")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "实例ID", dataType = "Long", required = true, paramType = "body"))
	public Result get(@RequestBody SysRole role) {
		if (role.getId() == null) {
			return Result.fail("参数不正确");
		}
		SysRole m = roleService.get(role.getId());
		m.setMenuIds(roleService.selectMenusByRoleId(m.getId()));
		BeanUtil.deleteProperties(m, CollUtil.newArrayList("menus"));
		return Result.success(m);
	}

	@RequiresPermissions("system:role:view")
	@PostMapping("/findPage")
	@ApiOperation("角色分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "params.nameLike", value = "角色名称模糊查询", dataType = "String", paramType = "body"),
			@ApiImplicitParam(name = "params.status", value = "状态", dataType = "String", paramType = "body") })
	public Result findPage(@RequestBody PageRequest request) {
		return Result.success(roleService.select(request));
	}
}
